home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************/
- /* Shelly: The ShellShapeGenerator by: */
- /* RANDi */
- /* (rschultz@informatik.uni-rostock.de) */
- /*****************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <memory.h>
-
- #include "shelly.h"
-
- int countw=0, counth=0;
-
- /********************************************/
- /*cot: */
- /********************************************/
- double cot(double in)
- {
- if (sin(in) == 0)
- return(0);
- else
- return(cos(in)/sin(in));
- }
-
-
- /********************************************/
- /*round: */
- /********************************************/
- double round(double in)
- {
- if((floor(in)-in) < 0)
- return(floor(in));
- else
- return(floor(in)+1);
- }
-
- /********************************************/
- /*myfree: */
- /********************************************/
- int myfree(struct punkt *anker1)
- {
- struct punkt *hp, *hpnew;
- if (anker1 == NULL)
- return(0);
-
- hp = anker1;
- hpnew = hp;
- do
- {
- hp = hpnew;
- hpnew = (*hp).next;
-
- cfree(hp);
- }
- while(hpnew);
-
- return(0);
- }
-
-
- /********************************************/
- /*mycopystr: */
- /********************************************/
- char *mycopystr(char *source,char *destination,char endchar)
- {
- int ende = 1;
- char *merk;
-
- merk = destination; /* merk points now on destination */
- while (ende)
- {
- if ((*source == '\0') || (*source == endchar))
- {
- ende = 0;
- }
- else
- {
- *destination = *source;
- destination++;
- source++;
- } /* if */
- } /* while */
- *destination = '\0';
- return(merk);
- }
-
-
- /********************************************/
- /*ReadInfile: */
- /********************************************/
-
- void ReadInfile(struct ShellyArguments *ShellyArgs,char *fin)
- {
- char readline[laenge], hilf[laenge], *hp;
- FILE *fp;
-
- (*ShellyArgs).output = POV;
-
- fp = fopen(fin,"r");
- if (fp != NULL)
- {
- fprintf(stderr,"Parsing:");
- while (fgets(readline,laenge,fp))
- {
- fprintf(stderr,".");
-
- hp = strstr(readline,"alpha:");
- if (hp != NULL)
- {
- mycopystr(hp+6,hilf,'\0');
- (*ShellyArgs).alpha = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"beta:");
- if (hp != NULL)
- {
- mycopystr(hp+5,hilf,'\0');
- (*ShellyArgs).beta = atof(hilf);
- } /* if */
- hp = strstr(readline,"phi:");
- if (hp != NULL)
- {
- mycopystr(hp+4,hilf,'\0');
- (*ShellyArgs).phi = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"my:");
- if (hp != NULL)
- {
- mycopystr(hp+3,hilf,'\0');
- (*ShellyArgs).my = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"omega:");
- if (hp != NULL)
- {
- mycopystr(hp+6,hilf,'\0');
- (*ShellyArgs).omega = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"smin:");
- if (hp != NULL)
- {
- mycopystr(hp+5,hilf,'\0');
- (*ShellyArgs).smin = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"smax:");
- if (hp != NULL)
- {
- mycopystr(hp+5,hilf,'\0');
- (*ShellyArgs).smax = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"sd:");
- if (hp != NULL)
- {
- mycopystr(hp+3,hilf,'\0');
- (*ShellyArgs).sd = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"A:");
- if (hp != NULL)
- {
- mycopystr(hp+2,hilf,'\0');
- (*ShellyArgs).A = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"a:");
- if (hp != NULL)
- {
- mycopystr(hp+2,hilf,'\0');
- (*ShellyArgs).a = atof(hilf);
- } /* if */
-
-
- hp = strstr(readline,"b:");
- if (hp != NULL)
- {
- mycopystr(hp+2,hilf,'\0');
- (*ShellyArgs).b = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"P:");
- if (hp != NULL)
- {
- mycopystr(hp+2,hilf,'\0');
- (*ShellyArgs).P = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"W1:");
- if (hp != NULL)
- {
- mycopystr(hp+3,hilf,'\0');
- (*ShellyArgs).W1 = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"W2:");
- if (hp != NULL)
- {
- mycopystr(hp+3,hilf,'\0');
- (*ShellyArgs).W2 = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"N:");
- if (hp != NULL)
- {
- mycopystr(hp+2,hilf,'\0');
- (*ShellyArgs).N = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"L:");
- if (hp != NULL)
- {
- mycopystr(hp+2,hilf,'\0');
- (*ShellyArgs).L = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"omin:");
- if (hp != NULL)
- {
- mycopystr(hp+5,hilf,'\0');
- (*ShellyArgs).omin = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"omax:");
- if (hp != NULL)
- {
- mycopystr(hp+5,hilf,'\0');
- (*ShellyArgs).omax = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"od:");
- if (hp != NULL)
- {
- mycopystr(hp+3,hilf,'\0');
- (*ShellyArgs).od = atof(hilf);
- } /* if */
-
- hp = strstr(readline,"POV");
- if (hp != NULL)
- {
- (*ShellyArgs).output = POV;
- } /* if */
-
- hp = strstr(readline,"RPL");
- if (hp != NULL)
- {
- (*ShellyArgs).output = RPL;
- } /* if */
-
- } /* while */
-
- fprintf(stderr,"ready!\n");
- if (fclose(fp) != 0)
- fprintf(stderr,"Error while closing file: %s!\n",fin);
- }
- else
- {
- fprintf(stderr,"Could not open datafile: %s!\n",fin);
- exit(5);
- }
- } /* ReadInfile */
-
-
- /********************************************/
- /* writepovtriangles: */
- /********************************************/
- int writepovtriangles(FILE *fp,struct punkt *anker1,struct punkt *anker2)
- {
- struct punkt *p11, *p12, *p21, *p22;
-
- if ((anker1 == NULL) || (anker2 == NULL))
- return(0);
-
- p11 = anker1;
- p21 = anker2;
- p12 = p11;
- p22 = p21;
-
- do
- {
- p11 = p12;
- p21 = p22;
- p12 = (*p11).next;
- p22 = (*p21).next;
-
- if ((p12 != NULL) && (p22 != NULL))
- {
- fprintf(fp,"triangle{<%.2f,%.2f,%.2f><%.2f,%.2f,%.2f><%.2f,%.2f,%.2f>\n",(*p11).x,(*p11).y,(*p11).z,(*p12).x,(*p12).y,(*p12).z,(*p21).x,(*p21).y,(*p21).z);
- fprintf(fp,"pigment{te}}\n");
-
- fprintf(fp,"triangle{<%.2f,%.2f,%.2f><%.2f,%.2f,%.2f><%.2f,%.2f,%.2f>\n",(*p21).x,(*p21).y,(*p21).z,(*p12).x,(*p12).y,(*p12).z,(*p22).x,(*p22).y,(*p22).z);
- fprintf(fp,"pigment{te}}\n");
- }
- }
- while((p12 != NULL) && (p22 != NULL));
-
- return(0);
- } /* writepovtriangles */
-
-
- /********************************************/
- /* writerplline: */
- /********************************************/
- int writerplline(FILE *fp,struct punkt *anker1)
- {
- struct punkt *p11, *p12;
-
- if (anker1 == NULL)
- return(0);
-
- p11 = anker1;
- p12 = p11;
-
- countw++; /* calc number of lines created */
-
- fprintf(fp,"%.2f %.2f %.2f\n",(*p11).x,(*p11).y,(*p11).z);
-
- do
- {
- p11 = p12;
- p12 = (*p11).next;
-
- if (p12 != NULL)
- {
- fprintf(fp,"%.2f %.2f %.2f\n",(*p12).x,(*p12).y,(*p12).z);
- }
- }
- while(p12 != NULL);
-
- return(0);
- } /* writerplline */
-
-
-
- /********************************************/
- /* writepovheader: reads in camera/light's */
- /* */
- /********************************************/
-
- void writepovheader(FILE *fp)
- {
- FILE *fp2;
- int rf;
-
- fp2 = fopen("povheader.pov","r");
-
- if (fp2 == NULL)
- {
- fprintf(stderr,"Cannot open File: 'shelly.pov'\n");
- fprintf(stderr,"Creating File with standard Camera/Light ... \n");
-
- fprintf(fp,"/* POV-Scenefile generated by Shelly1.0 */\n");
- fprintf(fp,"/* written by RANDi (rschultz@informatik.uni-rostock.de) */\n");
-
- fprintf(fp,"#declare te = pigment { color red 1 green 0 blue 0 }\n");
-
- fprintf(fp,"camera\n{\n location <0, 500, 500>\n");
- fprintf(fp," look_at <0, 0, 0>\n}\n");
-
- fprintf(fp,"object\n{\n light_source {\n");
- fprintf(fp," <250, 500, 500> color red 1 green 1 blue 1\n }\n");
- fprintf(fp,"}\n");
- fprintf(fp,"object\n{\n light_source {\n");
- fprintf(fp," <-250, 500, 500> color red 1 green 1 blue 1\n }\n");
- fprintf(fp,"}\n");
-
-
- }
- else
- {
-
- do
- {
- rf = fgetc(fp2);
- if (rf != -1) fputc(rf,fp);
- }
- while (rf != -1);
-
- fclose(fp2);
- } /* if */
- } /* writepovheader */
-
-
- /********************************************/
- /* writerplfinish: */
- /* */
- /********************************************/
-
- void writerplfinish(FILE *fp)
- {
- fprintf(fp,"%d\n",counth); /* height width of the mesh */
- fprintf(fp,"%d\n",countw);
-
- fprintf(fp,"3\n0\n255 255 255 0\n"); /* RGBA */
- fprintf(fp,"\"Shell\"\n0\n"); /* Name */
- fprintf(fp,"\"CEND\"\n");
- fprintf(fp,"C_MESH DROP\n");
-
- } /* writerplfinish */
-
-
- /**********************************************/
- /* RenderShell: */
- /* SSIA :) */
- /**********************************************/
-
- void RenderShell(struct ShellyArguments *ShellyArgs,char *fout)
-
- {
- struct punkt *List1anker, *List2anker, *L1p1, *L2p1, *L1p2, *L2p2;
-
- int countdown;
- double x,y,z,R,S,O;
- double my, Re, P, W1, W2, N, L, g, k;
- double smin, smax, sd, a, b, A, beta, phi, omega, alpha, omin, omax, od;
- FILE *fp;
-
- smin = (*ShellyArgs).smin*pi/180;
- smax = (*ShellyArgs).smax*pi/180;
- omin = (*ShellyArgs).omin*pi/180;
- omax = (*ShellyArgs).omax*pi/180;
- sd = (*ShellyArgs).sd*pi/180;
- od = (*ShellyArgs).od*pi/180;
-
- alpha = (*ShellyArgs).alpha*pi/180;
- beta = (*ShellyArgs).beta*pi/180;
- phi = (*ShellyArgs).phi*pi/180;
- omega = (*ShellyArgs).omega*pi/180;
- my = (*ShellyArgs).my*pi/180;
-
- a = (*ShellyArgs).a;
- A = (*ShellyArgs).A;
- N = (*ShellyArgs).N;
- W1 = (*ShellyArgs).W1*pi/180;
- W2 = (*ShellyArgs).W2*pi/180;
- P = (*ShellyArgs).P*pi/180;
- L = (*ShellyArgs).L;
- b = (*ShellyArgs).b;
-
- countdown = (fabs(omin)+fabs(omax))/od;
-
- S = smin;
- O = omin;
-
- fp = fopen(fout,"w");
- if (fp != NULL)
- {
- if ((*ShellyArgs).output != RPL)
- writepovheader(fp);
-
- fprintf(stderr,"Calculating... be patient!\n");
-
- for(S = smin;(S+sd)<smax;S=S+sd)
- {
- if (N == 0)
- g = 0;
- else
- g = (2*pi)/N*(O*N/(2*pi)-round(O*N/(2*pi)));
-
- Re = pow((pow(a,-2.0)*(cos(S)*cos(S))+pow(b,-2.0)*(sin(S)*sin(S))),-0.5);
-
- if (N == 0)
- k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1));
- else
- k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1))*exp(-(2*g/W2)*(2*g/W2));
-
- R = Re + k;
-
- /* alternatively: */
- /* R = pow((pow(a,2.0)*pow(cos(S),2.0)+pow(b,2.0)*pow(sin(S),2.0)),0.5); */
-
- x = (A*sin(beta)*cos(O)+R*cos(S+phi)*cos(O+omega)-R*sin(my)*sin(S+phi)*sin(O))*exp(O*cot(alpha));
- y = (-1*A*sin(beta)*sin(O)-R*cos(S+phi)*sin(O+omega)-R*sin(my)*sin(S+phi)*cos(O))*exp(O*cot(alpha));
- z = (-1*A*cos(beta)+R*sin(S+phi)*cos(my))*exp(O*cot(alpha));
-
- if ((L1p1 = calloc(1,sizeof(struct punkt))) == NULL)
- {
- fprintf(stderr,"Shelly: NOT ENOUGH MEMORY!!! ... exiting ... !\n");
- fclose(fp);
- myfree(List2anker);
- myfree(List1anker);
- exit(5);
- } /* if */
-
- if (S == smin)
- List1anker = L1p1;
- else
- (*L1p2).next = L1p1;
- L1p2 = L1p1;
-
- (*L1p1).x = x;
- (*L1p1).y = y;
- (*L1p1).z = z;
-
- } /* for */
-
- if ((*ShellyArgs).output == RPL)
- writerplline(fp,List1anker);
-
- for(O = omin+od;(O+od)<omax;O=O+od)
- {
-
- fprintf(stderr,"%d\r",countdown--); /* produce a countdown */
- fflush(stderr);
-
- counth = 0;
-
- for(S = smin;(S+sd)<smax;S=S+sd)
- {
-
- counth++; /* calc number of points per line */
-
- if (N == 0)
- g = 0;
- else
- g = (2*pi)/N*(O*N/(2*pi)-round(O*N/(2*pi)));
-
- Re = pow((pow(a,-2.0)*(cos(S)*cos(S))+pow(b,-2.0)*(sin(S)*sin(S))),-0.5);
- if (N == 0)
- k = L*exp(-(2*(S-P)/W1)*(2*(S-P)/W1));
- else
- k = L*exp(-1*(2*(S-P)/W1)*(2*(S-P)/W1))*exp(-1*(2*g/W2)*(2*g/W2));
-
-
- R = Re + k;
-
- /* alternatively: */
- /* R = pow((pow(a,2.0)*pow(cos(S),2.0)+pow(b,2.0)*pow(sin(S),2.0)),0.5); */
-
- x = (A*sin(beta)*cos(O)+R*cos(S+phi)*cos(O+omega)-R*sin(my)*sin(S+phi)*sin(O))*exp(O*cot(alpha));
- y = (-1*A*sin(beta)*sin(O)-R*cos(S+phi)*sin(O+omega)-R*sin(my)*sin(S+phi)*cos(O))*exp(O*cot(alpha));
- z = (-1*A*cos(beta)+R*sin(S+phi)*cos(my))*exp(O*cot(alpha));
-
- if ((L2p1 = calloc(1,sizeof(struct punkt))) == NULL)
- {
- fprintf(stderr,"Shelly: NOT ENOUGH MEMORY!!! ... exiting ... !\n");
- fclose(fp);
- myfree(List1anker);
- myfree(List1anker);
- exit(5);
- } /* if */
-
- if (S == smin)
- List2anker = L2p1;
- else
- (*L2p2).next = L2p1;
- L2p2 = L2p1;
-
- (*L2p1).x = x;
- (*L2p1).y = y;
- (*L2p1).z = z;
-
-
- } /* for */
-
- if ((*ShellyArgs).output != RPL)
- writepovtriangles(fp,List1anker,List2anker);
- else
- writerplline(fp,List2anker);
-
- myfree(List1anker);
- List1anker = List2anker;
-
-
- } /* for */
-
- if ((*ShellyArgs).output == RPL)
- writerplfinish(fp);
-
-
- fprintf(stderr,"Calculation complete ... !\n");
-
- myfree(List1anker);
-
- if (fclose(fp) != 0)
- fprintf(stderr,"Error while closing file: %s!\n",fout);
- }
- else
- fprintf(stderr,"Could not open outfile: %s!\n",fout);
-
- } /* RenderShell */
-
-
-
-
- /********************************************/
- /*main: */
- /********************************************/
- int main(int ac,char **av)
-
- {
-
- struct ShellyArguments ShellyArgs;
-
- if (ac < 3)
- {
- fprintf(stderr,"Expected atleast 2 Arguments!\nUSAGE:");
- fprintf(stderr," 'shelly infilename outfilename'\n");
- exit(0);
- } /* if */
-
- ReadInfile(&ShellyArgs,av[1]);
-
- RenderShell(&ShellyArgs,av[2]);
-
- exit(0);
- }
-